#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化版Gitee SSH密钥生成器
"""

import subprocess
import os
from pathlib import Path


def generate_gitee_ssh_key():
    """生成Gitee SSH密钥的简化函数"""
    
    print("🚀 开始生成Gitee SSH密钥...")
    
    # SSH目录路径
    ssh_dir = Path.home() / '.ssh'
    key_path = ssh_dir / 'id_ed25519'
    pub_key_path = ssh_dir / 'id_ed25519.pub'
    
    # 创建SSH目录（如果不存在）
    ssh_dir.mkdir(mode=0o700, exist_ok=True)
    
    try:
        # 生成SSH密钥
        print("正在生成SSH密钥...")
        subprocess.run([
            'ssh-keygen',
            '-t', 'ed25519',
            '-C', 'Gitee SSH Key',
            '-f', str(key_path),
            '-N', ''  # 无密码
        ], check=True)
        
        print("✅ SSH密钥生成成功!")
        
        # 读取并显示公钥
        if pub_key_path.exists():
            with open(pub_key_path, 'r') as f:
                public_key = f.read().strip()
            
            print("\n" + "="*60)
            print("🔓 您的Gitee SSH公钥:")
            print("="*60)
            print(public_key)
            print("="*60)
            
            # 保存到当前目录
            with open('gitee_ssh_public_key.txt', 'w') as f:
                f.write(public_key)
            
            print(f"\n💾 公钥已保存到: {os.path.abspath('gitee_ssh_public_key.txt')}")
            print("\n📋 请复制上面的公钥内容，然后:")
            print("1. 登录 https://gitee.com")
            print("2. 进入 设置 -> SSH公钥")
            print("3. 添加新的SSH公钥")
            print("4. 粘贴公钥内容并保存")
            
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"❌ 生成失败: {e}")
        return False
    except FileNotFoundError:
        print("❌ 未找到ssh-keygen命令")
        print("请确保已安装Git或OpenSSH")
        return False
    except Exception as e:
        print(f"❌ 发生错误: {e}")
        return False


if __name__ == "__main__":
    try:
        generate_gitee_ssh_key()
        input("\n按回车键退出...")
    except KeyboardInterrupt:
        print("\n操作已取消") 